package org.qydjk.console.controller.wechatPublicNumber;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.WechatPublicMessageTemplate;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Lanan on 2017/12/28 0028.
 */
@Controller
@RequestMapping("/wechat/public/number/message/template")
public class WechatPublicMessageTemplateController extends BaseController {

    @RequestMapping("/index.do")
    public String wechatPublicMessageTemplateIndex() {
        return "/wechatPublicNumber/messageTemplate/wechatPublicMessageTemplate";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object wechatPublicMessageTemplateList(int page, int rows) {
        List<WechatPublicMessageTemplate> wechatPublicMessageTemplates = new ArrayList<WechatPublicMessageTemplate>();
        Integer total = 0;

        String sql = "SELECT * FROM qy_wechat_public_message_template ORDER BY update_time DESC";
        String countSql = "SELECT COUNT(id) FROM qy_wechat_public_message_template";
        try {
            wechatPublicMessageTemplates = baseService.findBySQL(sql, WechatPublicMessageTemplate.class, new HibernatePage(page, rows));
            total = baseService.getRecordCountSQL(countSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(total, wechatPublicMessageTemplates);
    }

    @RequestMapping("/insert.do")
    @ResponseBody
    public Object wechatPublicMessageTemplateInsert(WechatPublicMessageTemplate wechatPublicMessageTemplate) {
        wechatPublicMessageTemplate.setCreateTime(new Date());
        return hangleInsertAndUpdate(true, wechatPublicMessageTemplate);
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public Object wechatPublicMessageTemplateUpdate(WechatPublicMessageTemplate wechatPublicMessageTemplate) {
        return hangleInsertAndUpdate(false, wechatPublicMessageTemplate);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object wechatPublicMessageTemplateDelete(WechatPublicMessageTemplate wechatPublicMessageTemplate) {
        JSONObject resp = new JSONObject();
        try {
            baseService.delete(wechatPublicMessageTemplate);
            resp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    /**
     * 处理添加和修改
     * @param isInsert true:添加 false:修改
     * @param wechatPublicMessageTemplate
     * @return
     */
    private Object hangleInsertAndUpdate(boolean isInsert, WechatPublicMessageTemplate wechatPublicMessageTemplate) {
        JSONObject resp = new JSONObject();
        wechatPublicMessageTemplate.setUpdateTime(new Date());
        WechatPublicMessageTemplate oldWPMT = new WechatPublicMessageTemplate();
        String sql = "SELECT COUNT(id) FROM qy_wechat_public_message_template WHERE template_id = '" + wechatPublicMessageTemplate.getTemplateId() + "'";
        try {
            if (!isInsert) {
                oldWPMT = baseService.get(WechatPublicMessageTemplate.class, wechatPublicMessageTemplate.getId().intValue());
            }
            Integer count = baseService.getRecordCountSQL(sql);
            if (count > 0) {
                resp.put("success", false);
                resp.put("msg", "该短信模板已存在,不可重复提交!");
                if (isInsert) {
                    return JSON.toJSON(resp);
                } else {
                    if (!oldWPMT.getTemplateId().equals(wechatPublicMessageTemplate.getTemplateId())) {
                        return JSON.toJSON(resp);
                    }
                }
            }
            if (isInsert) {
                return insertNewInfo(resp, wechatPublicMessageTemplate);
            } else {
                return updateNewInfo(resp, wechatPublicMessageTemplate, oldWPMT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    private Object insertNewInfo(JSONObject resp, WechatPublicMessageTemplate wechatPublicMessageTemplate) {
        try {
            resp.put("success", true);
            resp.put("msg", "添加");
            baseService.save(wechatPublicMessageTemplate);
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

    /**
     * 修改
     * @param resp
     * @param wechatPublicMenu
     * @param oldWPM
     * @return
     */
    private Object updateNewInfo(JSONObject resp, WechatPublicMessageTemplate wechatPublicMenu, WechatPublicMessageTemplate oldWPM) {
        try {
            BeanUtil.copyProperties(wechatPublicMenu, oldWPM);
            baseService.update(oldWPM);
            resp.put("success", true);
            resp.put("msg", "修改");
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }

}
